require 'json'
class FileModel
  attr_reader :id,:name
  attr_writer :id,:name
end
class Command 
  include JSON
  attr_reader :type,:ori_file_model,:aim_file_model
  attr_writer :type,:ori_file_model,:aim_file_model
  def initialize(json_str)
      json = JSON.parse(json_str) 
      ori_file_json = json["oriFileModel"]
      aim_file_json = json['aimFileModel']
      @type = json["type"]
      @ori_file_model = FileModel.new
      @aim_file_model = FileModel.new

      @ori_file_model.id = ori_file_json["id"]
      @ori_file_model.name = ori_file_json["name"]

      @aim_file_model.id = aim_file_json["id"]
      @aim_file_model.name = aim_file_json["name"]
      
   end
end
class Result
  attr_reader :success,:message,:file_model
  attr_writer :success,:message,:file_model
  def to_json(*args)
      {
        'success'  =>@success,
        'message'  =>@message,
        'file'  =>@file_model.to_json
      }.to_json(*args)
    end
end
class CommandController < ApplicationController
   include JSON
   before_filter :authorize
   #scaffold :langzheng
   def exec
      command = Command.new(params["data"]) 
      result = Result.new
      if command.type=='filepastebycopy'
        result =filepastebycopy(command) 
      end
      if command.type=='filepastebycut'
        result =filepastebycut(command)
      end
      if command.type=='filemodify'
        result =filemodify(command)       
      end
      if command.type=='foldermodify'
        result =filemodify(command)      
      end       
      if command.type=='filedelete'
        result = filedelete(command)
      end
      if command.type=='foldercreate'
        result = foldercreate(command)
      end
      render :text=> generate(result)
   end 
   private
   def filepastebycopy(command) 
     result = Result.new
     file_model = Langzheng.copy(command.ori_file_model.id, command.aim_file_model.id)
     
     result.success = true
     result.message ="ok" 
     result.file_model=file_model
     return result
   end
   def filepastebycut(command) 
     result = Result.new
     file_model = Langzheng.cut(command.ori_file_model.id, command.aim_file_model.id)
     
     result.success = true
     result.message ="ok" 
     result.file_model=file_model
     return result
   end
   def filemodify(command) 
     result = Result.new
     file_model = Langzheng.modify_name(command.ori_file_model.id, command.aim_file_model.name)  
     result.success = true
     result.message ="ok" 
     result.file_model=file_model
     return result
   end
   def filedelete(command) 
     result = Result.new
     Langzheng.file_delete(command.ori_file_model.id)  
     result.success = true
     result.message ="ok"  
     return result
   end
   def foldercreate(command) 
     result = Result.new
     Langzheng.folder_create(command.ori_file_model.name,command.aim_file_model.id)  
     result.success = true
     result.message ="ok"  
     return result
   end
end
